//定义关于counter的store
import { defineStore } from "pinia";

import { UserInfoType } from "@/api/login/type";

const enum RoleEnum {
  SUPER = "super",
  TEST = "test",
}

interface UserState {
  userInfo?: UserInfoType | null;
  token?: string;
  roleList: RoleEnum[];
  sessionTimeout?: boolean;
  lastUpdateTime: number;
  isLogin: boolean;
  class: string;
}

//defineStore 是返回一个函数 函数命名最好有use前缀，根据函数来进行下一步操作
const useUserStore = defineStore("userinfo", {
  state: (): UserState => ({
    userInfo: null,
    token: undefined,
    roleList: [],
    sessionTimeout: false,
    lastUpdateTime: 0,
    isLogin: false,  
    class: "",
  }),
  getters: {
    getLoginStatus(): boolean {
      // 判断用户是否登录
      const userinfo = localStorage.getItem("userinfo");
      if (userinfo && userinfo !== "undefined") {
        this.userInfo = JSON.parse(userinfo);
        this.isLogin = true;
      }
      return this.isLogin;
    },
    getUser_isTeacher(): boolean {
      return   parseInt( '' + this.userInfo?.group_id) >1  ? true : false
    }
  },
  actions: {
    setLogin(v: UserInfoType) {
      this.userInfo = v;
      localStorage.setItem("userinfo", JSON.stringify(v));
      this.isLogin = true;
    },
    clearUserInfo() {
      localStorage.removeItem("userinfo");
      this.isLogin = false;
    },
  },
});

export default useUserStore;
